<?php
/*
* @Date: 2021-01-28 17:14:59
* @名称: 后台 · 漫画 · 获取 · 获取漫画列表 · 下一页
* @版本: 0.01
* @作者: 初雪桜
* @邮箱: 202184199@qq.com
* @最后编辑: 初雪桜
 * @LastEditTime: 2021-01-28 23:44:43
 * @FilePath: /Site/app/useradmin/controller/Comics/Get/Method/GetNewPage.php
*/

namespace app\useradmin\controller\Comics\Get;


use app\BaseController;
use think\facade\Db;

class GetNewPage extends BaseController
{
    /**
     * @方法名: 获取随机一页数据
     * @作者: 初雪桜
     * @信息: 查询完漫画列表 按条件搜索 换页
     * @param Json $user_info 用户信息
     * @param Int $page 第几页
     * @param String $title 漫画标题
     * @param int $status 漫画状态
     * @return Json
     */
    public function GetNewPage($user_info, $page, $title, $status)
    {

        $selectWhere = [];
        array_push($selectWhere, ["book_list.delete", '=', 0]); //不能被删除
        if ($title != null && $title != "") {
            array_push($selectWhere, ["book_list.title", 'like', "%$title%"]);
            $ExtendedParameters = ",$title";
        } else {
            $ExtendedParameters = ",undefined";
        }
        switch ($status) {
            case 0:
                #正常的
                array_push($selectWhere, ["book_list.state", '=', 1]);
                $ExtendedParameters .= "";
                break;
            case 1:
                #待审核
                array_push($selectWhere, ["book_list.state", '=', 0]);
                array_push($selectWhere, ["book_list.exp_state", '=', 1]);
                break;
            case 2:
                #待删除
                array_push($selectWhere, ["book_list.state", '=', 0]);
                array_push($selectWhere, ["book_list.exp_state", '=', 2]);
                break;
            default:
                return errorJsonReturn("Parameter validation failed");
                break;
        }
        $seleNumber = 20;
        //每次查询的数量
        try {
            $data_list =  Db::table('author_and_chinese_team')
                ->where([
                    "author_and_chinese_team.user_id" => $user_info['_id'] //@a 查询 我所属于的漫画
                ])
                ->alias('author_and_chinese_team')
                ->join("book_list book_list", "book_list._id = author_and_chinese_team.book_id")
                ->join('user_info user_info', 'user_info._id = book_list.user_id')
                ->join('book_info book_info', 'book_info._id = book_list._id')
                ->limit($page * $seleNumber - $seleNumber, $seleNumber)
                ->where($selectWhere)
                ->field([
                    "book_list.title",
                    #标题
                    "book_list._id",
                    #漫画id
                    "book_info.categories",
                    #分类
                    "book_info.tags",
                    #标签
                    "book_list.created_at",
                    #创建时间
                    "book_list.file_server",
                    #服务器地址
                    "book_list.path",
                    #路径
                    "book_list.state",
                    #状态 1正常 0异常
                    "book_list.exp_state",
                    #扩展状态 1待站长审核 2待删除
                    "user_info.name as author",
                    #作者姓名
                ])
                ->order('book_list.id desc')
                ->select();
        } catch (\Throwable $th) {
            dump($th);
        }

        $data_list_count =  Db::table('book_list')
            ->where($selectWhere)
            ->count();
        // dump($data_list);
        // return view('Comics/ComicsList/ComicsList', ["comicsList" => $data_list, 'springboard' => "http://111.beaa.xyz/?url="]);
        return json([
            "comicsList" => $data_list,
            "ComicsListCount" => $data_list_count,
            "url" => 'http://' . $_SERVER['HTTP_HOST'] . "/",
            "page" => generatePaging($data_list_count, $page, $ExtendedParameters),
            'springboard' => config("globalsettings.PictureSpringboard"),
        ]);
    }
}
